#include "newtaskdialog.h"
#include "taskrecord.h"

#include <QGridLayout>
#include <QLabel>
#include <QDir>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#include <QFileDialog>
#include <QSlider>
#include <QHBoxLayout>

NewTaskDialog::NewTaskDialog(QWidget *parent) :
    QDialog(parent)
{
    QGridLayout *layout = new QGridLayout(this);

    // URL field
    layout->addWidget(new QLabel("URL", this), 0, 0);
    urlEdit = new QLineEdit("", this);
    urlEdit->setMaximumHeight(25);
    layout->addWidget(urlEdit, 0, 1, 1, 2);

    // Path field
    layout->addWidget(new QLabel("Path", this), 1, 0);
    pathEdit = new QLineEdit(QDir::homePath(), this);
    pathEdit->setMaximumHeight(25);
    pathEdit->setReadOnly(true);
    layout->addWidget(pathEdit, 1, 1);
    QPushButton* chooseButton = new QPushButton("C&hoose", this);
    layout->addWidget(chooseButton, 1, 2);
    connect(chooseButton, SIGNAL(clicked()), this, SLOT(chooseFolder()));

    // Priority slider
    layout->addWidget(new QLabel("Priority", this), 2, 0);
    prioritySlider = new QSlider(Qt::Horizontal, this);
    prioritySlider->setMaximum(5);
    prioritySlider->setMinimum(-5);
    prioritySlider->setTickPosition(QSlider::TicksBelow);
    layout->addWidget(prioritySlider, 2, 1, 1, 2);
    layout->addWidget(new QLabel("Lowest", this), 3, 1);
    layout->addWidget(new QLabel("Highest", this), 3, 2, Qt::AlignRight);

    // Control buttons
    QHBoxLayout* controls = new QHBoxLayout(this);
    QPushButton* okButton = new QPushButton("&OK", this);
    QPushButton* cancelButton = new QPushButton("&Cancel", this);
    controls->addStretch(1);
    controls->addWidget(okButton);
    controls->addWidget(cancelButton);
    controls->addStretch(1);
    layout->addLayout(controls, 4, 0, 1, 3);
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}

TaskRecord* NewTaskDialog::getTask()
{
    return new TaskRecord(urlEdit->text(), pathEdit->text(), prioritySlider->value());
}

void NewTaskDialog::chooseFolder() {
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
        "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    pathEdit->setText(dir);
}
